<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getDictTypeList" row-key="dictId">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'dict_type_save'" type="primary" :icon="CirclePlus" @click="openDrawer('新增')">新增字典</el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button v-auth="'dict_type_save'" type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)">
          编辑
        </el-button>
        <el-button v-auth="'dict_type_del'" type="danger" link :icon="Delete" @click="deleteAdminUser(scope.row)">
          删除
        </el-button>
        <el-button v-auth="'dict_type_del'" type="primary" link :icon="List" @click="toDictData(scope.row.dictType)">
          字典值
        </el-button>
      </template>
    </ProTable>
    <DictTypeEdit ref="drawerRef" />
  </div>
</template>
<script setup lang="tsx" name="dictType">
import { ref, reactive } from "vue";
import { Support } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import DictTypeEdit from "@/views/support/dictType/DictTypeEdit.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { CirclePlus, Delete, EditPen, List } from "@element-plus/icons-vue";
import { getDictTypeList, saveDictType, delDictType } from "@/api/modules/support";
import { useHandleData } from "@/hooks/useHandleData";
import { useRouter } from "vue-router";

// ProTable 实例
const proTable = ref<ProTableInstance>();

// 表格配置项
const columns = reactive<ColumnProps<Support.DictType>[]>([
  { prop: "dictName", label: "字典名称", search: { el: "input" } },
  { prop: "dictType", label: "字典标识" },
  { prop: "remark", label: "备注" },
  { prop: "createdAt", label: "创建时间" },
  { prop: "updatedAt", label: "更新时间" },
  { prop: "operation", label: "操作", width: 250, fixed: "right" }
]);

// 删除权限资源
const deleteAdminUser = async (params: Support.DictType) => {
  await useHandleData(delDictType, { ids: params.dictId }, `删除【${params.dictName}】字典`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof DictTypeEdit> | null>(null);
const openDrawer = (title: string, row: Partial<Support.DictType> = {}) => {
  const params = {
    title,
    isView: title === "查看",
    row: { ...row },
    api: saveDictType,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
const router = useRouter();
//字典值
const toDictData = (dictType: number) => {
  router.push(`/support/dictData/${dictType}`);
};
</script>
